<template>
  <el-breadcrumb separator="/" style="margin: 20px 20px 20px 20px">
    <el-breadcrumb-item :to="'/'">首页</el-breadcrumb-item>
    <el-breadcrumb-item>协同指挥决策</el-breadcrumb-item>
    <el-breadcrumb-item>{{ breadcrumbName[0] }}</el-breadcrumb-item>
    <el-breadcrumb-item>{{ breadcrumbName[1] }}</el-breadcrumb-item>
  </el-breadcrumb>
  <div class="content-body">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      autocomplete="off"
      label-position="right"
      label-width="125px"
    >
      <div class="body-row">
        <div class="row-title">
          <!-- <div class="title-line"></div> -->
          <div class="title">事件上报</div>
          <example-picture />
        </div>
        <!-- 编辑上报 -->
        <template v-if="!route.query.status || isEdit">
          <div class="row-content">
            <div class="in-panel" style="border: none">
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="发生区域：" prop="terminal">
                    <el-radio-group v-model="formData.terminal">
                      <el-radio
                        :label="item.value"
                        v-for="(item, i) in platformList"
                        :key="i"
                        >{{ item.label }}</el-radio
                      >
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item
                    label="事件地点："
                    placeholder="请输入/地图选点"
                    prop="location"
                  >
                    <el-input v-model="formData.location" maxlength="50"/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="发生时间：" prop="eventTime">
                    <el-date-picker
                      v-model="formData.eventTime"
                      type="datetime"
                      placeholder="请选择"
                      format="YYYY-MM-DD HH:mm:ss"
                      value-format="YYYY-MM-DD HH:mm:ss"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="16">
                  <el-form-item label="事件类型：" prop="eventTypeId">
                    <el-cascader
                      v-model="formData.eventTypeId"
                      :options="eventTypes"
                      placeholder="请选择"
                      :props="{ value: 'id', label: 'name' }"
                      @change="eventTypeChange"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="事件紧急程度：" prop="emergencyLevel">
                    <el-select
                      v-model="formData.emergencyLevel"
                      placeholder="请选择"
                      @change="emergencyLevelChange"
                    >
                      <el-option
                        v-for="item in eventUrgency"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="24">
                  <el-form-item label="事件描述：" prop="description">
                    <el-input
                      v-model="formData.description"
                      maxlength="500"
                      :rows="5"
                      show-word-limit
                      type="textarea"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="后果/影响：" prop="result">
                    <el-select
                      v-model="formData.result"
                      placeholder="请选择"
                      @change="resultChange"
                    >
                      <el-option
                        v-for="item in consequence"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="事件级别：" prop="eventLevel">
                    <el-select
                      v-model="formData.eventLevel"
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="item in eventLevel"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="区域处置单位：" prop="processGroupId">
                    <el-tree-select
                      ref="treeRef"
                      v-model="formData.processGroupId"
                      :data="unitList"
                      node-key="organizationId"
                      default-expand-all
                      check-strictly
                      show-checkbox
                      :props="{ label: 'organizationName' }"
                    >
                    </el-tree-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="附件上传：">
                    <upload-files
                      :limit="10"
                      v-model="formData.files"
                      :accept="[
                        'jpg',
                        'jpeg',
                        'png',
                        'gif',
                        'bmp',
                        'svg',
                        'doc',
                        'docx',
                      ]"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="事件名称：" prop="eventName">
                    <el-input v-model="formData.eventName" maxlength="50" />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
        </template>

        <!-- 查看上报信息  -->
        <template v-if="route.query.status && !isEdit">
          <div class="row-content">
            <div class="in-panel" style="border: none">
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="发生区域：">
                    <span>{{
                      handleTranslate(platformList, infoData.terminal)
                    }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="事件地点：">
                    <span>{{ infoData.location }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="发生时间：">
                    <span>{{ infoData.eventTime }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="16">
                  <el-form-item label="事件类型：">
                    <span>{{
                      findTypeName(eventTypes, infoData.eventTypeId)?.join(
                        " - "
                      )
                    }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="事件紧急程度：">
                    <span>{{
                      handleTranslate(eventUrgency, infoData.emergencyLevel)
                    }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="16">
                  <el-form-item label="事件描述：">
                    <span>{{ infoData.description }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="来源：">
                    <span>{{ infoData.source }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="后果/影响：">
                    <span>{{
                      handleTranslate(consequence, infoData.result)
                    }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="事件级别：">
                    <span>{{
                      handleTranslate(eventLevel, infoData.eventLevel)
                    }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="区域处置单位：">
                    <span>{{ infoData.processGroup?.name }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="附件上传：">
                    <upload-files
                      disabled
                      v-model="infoData.files"
                      :accept="[
                        'jpg',
                        'jpeg',
                        'png',
                        'gif',
                        'bmp',
                        'svg',
                        'doc',
                        'docx',
                      ]"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="事件名称：">
                    <span>{{ infoData.eventName }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
        </template>
      </div>

      <!-- 已审核展示 响应和退回信息 待接收展示 已上报信息 -->
      <template v-for="(item, index) in infoData.list" :key="index">
        <div class="body-row" v-if="item.status == 20">
          <div class="row-title">
            <!-- <div class="title-line"></div> -->
            <div class="title">事件响应与下发</div>
          </div>
          <div class="row-content">
            <div class="in-panel" style="border: none">
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="事件级别：">
                    <span>{{
                      handleTranslate(eventLevel, item.eventLevel)
                    }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="区域处置单位：">
                    <span>{{ item.processGroup?.name }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="附件上传：">
                    <upload-files
                      disabled
                      v-model="item.files"
                      :accept="[
                        'jpg',
                        'jpeg',
                        'png',
                        'gif',
                        'bmp',
                        'svg',
                        'doc',
                        'docx',
                      ]"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
        <div class="body-row" v-if="item.status == 11">
          <div class="row-title">
            <!-- <div class="title-line"></div> -->
            <div class="title">事件退回</div>
          </div>
          <div class="row-content">
            <div class="in-panel" style="border: none">
              <el-row :gutter="10">
                <el-col :span="8">
                  <el-form-item label="退回人：">
                    <span>{{ item.creator?.name }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="退回时间：">
                    <span>{{ item.createTime }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="退回原因：">
                    <span>{{ item.reason }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </template>

      <!--  待审核展示 -->
      <template v-if="route.query.status == 10">
        <div class="body-row">
          <div class="row-title">
            <!-- <div class="title-line"></div> -->
            <div class="title">视频监控</div>
          </div>
          <div class="row-content">
            <div class="search">
              <el-row :gutter="20">
                <span>类型：</span>
                <el-select />
                <span>路名：</span>
                <el-input />
              </el-row>
            </div>
            <div class="monitoring">
              <div class="catalogue">
                <div class="video-type">
                  <div style="border-right: 2px solid #4f91f3">视频类型1</div>
                  <div>视频类型2</div>
                  <div>视频类型3</div>
                  <div>视频类型4</div>
                  <div>视频类型5</div>
                </div>
                <div class="video-name">
                  <div style="border: 1px solid #4f91f3">视频路名1</div>
                  <div>视频路名2</div>
                  <div>视频路名3</div>
                  <div>视频路名4</div>
                  <div>视频路名5</div>
                  <div>视频路名6</div>
                  <div>视频路名7</div>
                </div>
              </div>
              <div class="video">
                <div><img :src="image.loginBg" alt="" /></div>
                <div class="icon">
                  <div>
                    <el-icon color="#4F91F3" size="30">
                      <Postcard />
                    </el-icon>
                  </div>
                  <div>
                    <el-icon color="#4F91F3" size="30">
                      <VideoPause />
                    </el-icon>
                  </div>
                  <div>
                    <el-icon color="#4F91F3" size="30">
                      <Picture />
                    </el-icon>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>

      <div class="bottom-btn" v-if="!route.query.status">
        <el-button plain @click="router.go(-1)">取消</el-button>
        <el-button
          type="primary"
          style="margin-left: 100px"
          :loading="loadingBtn"
          @click="onSubmit('add')"
          >确定</el-button
        >
      </div>
      <div class="bottom-btn" v-if="route.query.status == 10">
        <el-button plain @click="router.go(-1)">取消</el-button>
        <el-button
          plain
          style="margin-left: 50px"
          type="danger"
          @click="isRollbackDialog = true"
          >退回</el-button
        >
        <el-button
          v-if="!isEdit"
          type="primary"
          plain
          style="margin-left: 50px; background-color: #ecf5ff"
          @click="isEdit = true"
          >编辑</el-button
        >
        <el-button
          v-else
          type="primary"
          plain
          style="margin-left: 50px; background-color: #ecf5ff"
          @click="onSubmit('update')"
          :loading="loadingBtn"
          >保存</el-button
        >
        <el-button
          type="primary"
          style="margin-left: 50px"
          @click="isResponseDialog = true"
          >响应</el-button
        >
      </div>
      <div class="bottom-btn" v-if="route.query.status == 11">
        <el-button plain @click="router.go(-1)">关闭</el-button>
        <template v-if="creator.id == infoData?.creator?.id">
          <el-button
            v-if="!isEdit"
            style="margin-left: 50px"
            type="primary"
            @click="isEdit = true"
            >编辑</el-button
          >
          <el-button
            v-else
            style="margin-left: 50px"
            type="primary"
            @click="onSubmit('update')"
            :loading="loadingBtn"
            >提交</el-button
          >
        </template>
      </div>
      <div class="bottom-btn" v-if="route.query.status == 20">
        <el-button plain @click="router.go(-1)">关闭</el-button>
        <el-button
          type="primary"
          style="margin-left: 50px"
          @click="receptionDialog = true"
          :loading="loadingBtn"
          >接收</el-button
        >
      </div>
    </el-form>
  </div>
  <el-dialog v-model="isRollbackDialog" title="事件退回" width="30%">
    <el-form
      ref="rollbackRef"
      :model="rollbackForm"
      :rules="rollbackRules"
      label-position="right"
      label-width="100px"
    >
      <el-form-item label="退回原因：" prop="reason">
        <el-input
          v-model="rollbackForm.reason"
          maxlength="50"
          show-word-limit
          :rows="4"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="isRollbackDialog = false">取消</el-button>
        <el-button
          type="primary"
          :loading="loadingBtn"
          @click="handleSubmit('11')"
        >
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
  <el-dialog
    v-model="isResponseDialog"
    title="事件响应【上报与下发】"
    width="30%"
    class="responseDialog"
  >
    <el-form
      ref="responseRef"
      :model="responseForm"
      :rules="responseRules"
      label-position="right"
      label-width="130px"
    >
      <el-form-item label="后果/影响：" prop="result">
        <el-select v-model="responseForm.result" placeholder="请选择">
          <el-option
            v-for="item in consequence"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事件级别：" prop="eventLevel">
        <el-select v-model="responseForm.eventLevel" placeholder="请选择">
          <el-option
            v-for="item in eventLevel"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="区域处置单位：" prop="processGroupId">
        <el-tree-select
          ref="responseTreeRef"
          v-model="responseForm.processGroupId"
          :data="unitList"
          node-key="organizationId"
          default-expand-all
          check-strictly
          show-checkbox
          :props="{ label: 'organizationName' }"
        >
        </el-tree-select>
      </el-form-item>
      <el-form-item label="附件上传：">
        <upload-files
          :limit="10"
          v-model="responseForm.files"
          :accept="[
            'jpg',
            'jpeg',
            'png',
            'gif',
            'bmp',
            'svg',
            'doc',
            'docx',
          ]"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="isResponseDialog = false">取消</el-button>
        <el-button
          type="primary"
          :loading="loadingBtn"
          @click="handleSubmit('20')"
        >
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>

  <el-dialog v-model="receptionDialog" title="确认接收" width="30%">
    <div style="display: flex; flex-direction: column; align-items: center">
      <el-icon color="#409EFC" size="50">
        <Warning />
      </el-icon>
      <div style="margin-top: 20px">
        是否确认接收事件类型为【{{
          findTypeName(eventTypes, infoData.eventTypeId)?.join(" - ")
        }}】的事件？
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="receptionDialog = false">取消</el-button>
        <el-button type="primary" :loading="loadingBtn" @click="confirmReceipt">
          确认接收
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onBeforeMount, computed, toRefs } from "vue";
import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";
import { image } from "@/assets/index";
import {
  eventReport,
  reportInfo,
  approveInfo,
  addEventReport,
  updateEventReport,
  getTree,
  getConsequence,
} from "@/api/collaborativeCommand/eventEscalation";
import { getResource } from "@/api/resource";
import { receive } from "@/api/collaborativeCommand/collaborativeDisposal";
import { ElMessage } from "element-plus";
import uploadFiles from "@/components/UploadFiles/index.vue";
import examplePicture from "../components/examplePicture.vue";
import { handleTranslate, findTypeName } from "../components/common.js";
import { moment } from "moment";

const route = useRoute();
const router = useRouter();
//页面数据
const infoData = reactive({
  list: [],
});
const formatDate=(dateObj) =>{
  const year = dateObj.getFullYear();
  const month = ("0" + (dateObj.getMonth() + 1)).slice(-2); // 注意：0 是一月份，11 是十二月份。
  const date = ("0" + dateObj.getDate()).slice(-2);
  const hours = ("0" + dateObj.getHours()).slice(-2);
  const minutes = ("0" + dateObj.getMinutes()).slice(-2);
  const seconds = ("0" + dateObj.getSeconds()).slice(-2);
  
  return year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
}

const now = new Date();
//上报表单数据
const formData = reactive({
  eventTime: formatDate(now),
  files: [],
});
const rollbackForm = reactive({}); //退回表单
const responseForm = reactive({}); //响应表单
const isRollbackDialog = ref(false); //退回弹窗
const isResponseDialog = ref(false); //响应弹窗
const formRef = ref(); //上报表单ref
const rollbackRef = ref(); //退回表单ref
const responseRef = ref(); //响应表单ref
const receptionDialog = ref(false); //确认接收弹窗

const rules = reactive({
  //上报校验
  terminal: [{ required: true, message: "请选择发生区域", trigger: "change" }],
  location: [{ required: true, message: "请输入事件地点", trigger: "blur" }],
  eventTime: [{ required: true, message: "请选择发生时间", trigger: "blur" }],
  eventTypeId: [
    { required: true, message: "请选择事件类型", trigger: "change" },
  ],
  description: [{ required: true, message: "请输入事件描述", trigger: "blur" }],
  result: [{ required: true, message: "请选择后果/影响", trigger: "change" }],
  eventLevel: [
    { required: true, message: "请选择事件级别", trigger: "change" },
  ],
  processGroupId: [
    { required: true, message: "请选择区域处置单位", trigger: "change" },
  ],
});
//退回校验
const rollbackRules = reactive({
  reason: [{ required: true, message: "请输入退回原因", trigger: "blur" }],
});
//响应校验
const responseRules = reactive({
  result: [{ required: true, message: "请选择后果/影响", trigger: "change" }],
  eventLevel: [
    { required: true, message: "请选择事件级别", trigger: "change" },
  ],
  processGroupId: [
    { required: true, message: "请选择区域处置单位", trigger: "change" },
  ],
});

const loadingBtn = ref(false);
const isEdit = ref(false); //是否编辑状态
const treeRef = ref(); //上报区域处置单位ref
const responseTreeRef = ref(); //响应区域处置单位ref

// 数据字典
const platformList = ref([]); //发生区域
const eventTypes = ref([]); //事件类型
const eventUrgency = ref([]); //事件紧急程度
const consequence = ref([]); //后果/影响
const eventLevel = ref([]); //事件级别
const unitList = ref([]); //区域处置单位

const isHandle = ref(false); //是否手动操作了事件紧急程度

// 用户信息
const store = useStore();
const userInfo = computed(() => store.state.user.userInfo);
const creator = reactive({
  id: userInfo.value.userId,
  name: userInfo.value.name,
  organId: userInfo.value.organId,
  organName: userInfo.value.organName,
});

// 面包屑
const breadcrumbName = computed(() => {
  const { status } = route.query;
  if (!status) {
    return ["事件上报管理", "新增事件上报"];
  } else if (status == 10) {
    return ["事件上报管理", "事件审核"];
  } else if (status == 11) {
    return ["事件上报管理", "事件退回"];
  } else if (status == 20) {
    return ["多部门协同处置", "事件接收"];
  }
});

const handleOrganizationId = (trees, id) => {
  for (let tree of trees) {
    if (tree.organizationId === id) {
      return tree;
    }
    if (tree.children.length) {
      const found = handleOrganizationId(tree.children, id);
      if (found) {
        return found;
      }
    }
  }
  return null; // 如果在所有树中都找不到匹配的id，就返回null
};

/**
 * 事件紧急程度
 */
const emergencyLevelChange = () => {
  isHandle.value = true;
};

/**
 * 后果影响
 */
const resultChange = (v) => {
  const obj = consequence.value.find((item) => item.value == v);
  // 处理事件级别
  const obj2 = eventLevel.value.find((item) => item.value == obj.eventLevel);
  // 存在就赋值
  if (typeof obj2 !== "undefined") {
    formData.eventLevel = obj.eventLevel;
  } else {
    formData.eventLevel = "";
  }

  // 处理区域处置单位
  const obj3 = handleOrganizationId(unitList.value, obj?.processGroup[0]?.id);
  // 存在就赋值
  if (obj3) {
    formData.processGroupId = obj?.processGroup[0]?.id;
  } else {
    formData.processGroupId = "";
  }

  // 事件紧急程度
  const obj4 = eventUrgency.value.find(
    (item) => item.value == obj.emergencyLevel
  );
  // 存在数据，并且没有手动选择
  if (obj4 && !isHandle.value) {
    formData.emergencyLevel = obj.emergencyLevel;
  }
};
/**
 * 事件类型change
 */
const eventTypeChange = (v) => {
  if (Array.isArray(v) && v.length > 0) {
    getConsequence({ eventTypeId: v[v.length - 1], enabled: 1 }).then((res) => {
      if (res.code == 1) {
        consequence.value = res.data?.map((item) => {
          return {
            value: item.id,
            label: item.result,
            eventLevel: item.eventLevel,
            processGroup: item.processGroup,
            emergencyLevel: item.emergencyLevel,
          };
        });

        // 判断新数据有没有之前选的值没有清空
        if (formData.result) {
          const obj = consequence.value.find(
            (item) => item.value == formData.result
          );
          if (typeof obj === "undefined") {
            formData.result = "";
          }
        }
      }
    });
  }
};

/**
 * 提交上报
 */
const onSubmit = (type) => {
  formRef.value.validate((valid) => {
    if (valid) {
      loadingBtn.value = true;
      const fun = type === "add" ? addEventReport : updateEventReport;
      fun({
        ...formData,
        eventTypeId: Array.isArray(formData.eventTypeId)
          ? formData.eventTypeId[formData.eventTypeId.length - 1]
          : formData.eventTypeId,
        processGroup: {
          id: treeRef.value.getCheckedNodes(false, false)[0]?.organizationId,
          name: treeRef.value.getCheckedNodes(false, false)[0]
            ?.organizationName,
        },
        creator,
        source: "人为上报", //pc上报写死
      })
        .then((res) => {
          if (res.code === "1") {
            router.go(-1);
          } else {
            ElMessage.error(res.message);
          }
          loadingBtn.value = false;
        })
        .catch((err) => {
          loadingBtn.value = false;
          ElMessage.error(err.message);
        });
    }
  });
};
/**
 * 确认接收
 */
const confirmReceipt = () => {
  loadingBtn.value = true;
  receive({ eventReportId: route.query.id, creator })
    .then((res) => {
      if (res.code === "1") {
        router.go(-1);
      } else {
        ElMessage.error(res.message || "接收失败");
      }
      loadingBtn.value = false;
    })
    .catch((err) => {
      loadingBtn.value = false;
      ElMessage.error(err.message || "接收失败");
    });
};
/**
 * 退回 响应
 */
const handleSubmit = async (status) => {
  let verify = false;
  let obj = {
    creator,
    status,
    eventReportId: route.query.id,
  };
  if (status === "11") {
    obj = { ...obj, ...rollbackForm };
    await rollbackRef.value.validate((valid) => {
      verify = valid;
    });
  } else {
    obj = {
      ...obj,
      ...responseForm,
      processGroup: {
        id: responseTreeRef.value.getCheckedNodes(false, false)[0]
          ?.organizationId,
        name: responseTreeRef.value.getCheckedNodes(false, false)[0]
          ?.organizationName,
      },
    };
    delete obj.processGroupId;
    await responseRef.value.validate((valid) => {
      verify = valid;
    });
  }
  if (!verify) {
    return;
  }
  loadingBtn.value = true;
  eventReport(obj)
    .then((res) => {
      if (res.code === "1") {
        router.go(-1);
      } else {
        ElMessage.error(res.message || "操作失败");
      }
      loadingBtn.value = false;
    })
    .catch((err) => {
      loadingBtn.value = false;
      ElMessage.error(err.message || "操作失败");
    });
};
/**
 * 获取事件上报信息 退回信息 响应信息
 */
const loadData = () => {
  reportInfo(route.query.id).then((res) => {
    if (res.code == 1) {
      Object.assign(infoData, res.data);
      Object.assign(formData, res.data);
      formData.processGroupId = res.data?.processGroup?.id;
      Object.assign(responseForm, {
        result: res.data?.result,
        eventLevel: res.data?.eventLevel,
        processGroupId: res.data?.processGroup?.id,
        files: res.data?.files,
      });

      approveInfo({ id: route.query.id }).then((res2) => {
        if (res2.code == 1) {
          infoData.list = res2.data;
        } else {
          ElMessage.error(res.message || "数据获取失败");
        }
      });
    } else {
      ElMessage.error(res.message || "数据获取失败");
    }
  });
};
/**
 * 加载数据字典
 */
const loadDict = () => {
  Promise.all([
    getResource("terminals"),
    getResource("event_rules_exigency_level"),
    getResource("event_rules_event_level"),
    getTree(),
    getConsequence({ enabled: 1 }),
  ]).then(([res1, res2, res3, res4, res5]) => {
    platformList.value = res1.data.properties.meta;
    eventUrgency.value = res2.data.properties.meta;
    eventLevel.value = res3.data.properties.meta;
    unitList.value = store.state.user.organTree;
    eventTypes.value = res4.data;
    consequence.value = res5.data?.map((item) => {
      return {
        value: item.id,
        label: item.result,
        eventLevel: item.eventLevel,
        processGroup: item.processGroup,
        emergencyLevel: item.emergencyLevel,
      };
    });
  });
};
const initData = () => {
  if (route.query.status) {
    loadData();
  }
  loadDict();
};
onBeforeMount(initData);
</script>

<style lang="less" scoped>
@import url("./index.less");
</style>
